package com.hjj.controller;

import com.hjj.beans.DictionaryValue;
import com.hjj.dto.ResultDTO;
import com.hjj.exception.CRUDException;
import com.hjj.exception.ParameterValidException;
import com.hjj.services.DictionaryValueServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.MessageSource;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Locale;
import java.util.UUID;

@RestController
@RequestMapping("/DictionaryValue")
public class DictionaryValueController {
    @Autowired
    @Qualifier("dictionaryValueServicesImp")
    DictionaryValueServices dictionaryValueServices;

    @Autowired
    MessageSource messageSource;

    @RequestMapping("getAll.action")
    public ArrayList<DictionaryValue> getAll(){
        return dictionaryValueServices.getAll();
    }

    @RequestMapping(value = "add.action", method = RequestMethod.POST)
    public ResultDTO add(@Valid DictionaryValue dictionaryValue, BindingResult bindingResult) throws CRUDException, ParameterValidException {
        // 验证器验证是否有异常
        if (bindingResult.hasErrors()){
            // 获取messageSource中的键
            String message = bindingResult.getFieldError().getDefaultMessage();
            // 根据messageSource键获取值
            throw new ParameterValidException(messageSource.getMessage(message, null, Locale.getDefault()));
        }


        // 添加UUID
        dictionaryValue.setId(UUID.randomUUID().toString().replace("-", ""));

        boolean add = dictionaryValueServices.add(dictionaryValue);

        if (!add){
            throw new CRUDException("字典值添加数据失败");
        }

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        return resultDTO;
    }


    @RequestMapping("del.action")
    public ResultDTO del(@RequestBody String[] ids) throws CRUDException {
        boolean del = dictionaryValueServices.del(ids);

        if (!del){
            throw new CRUDException("数据删除失败");
        }

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        return resultDTO;
    }

    @RequestMapping("editBefore.action")
    public DictionaryValue editBefore(String id){
        return dictionaryValueServices.get(id);
    }

    @RequestMapping(value = "edit.action", method = RequestMethod.POST)
    public ResultDTO edit(@Valid DictionaryValue dictionaryValue, BindingResult bindingResult) throws ParameterValidException, CRUDException {
        // 验证器验证参数是否有异常
        if (bindingResult.hasErrors()){
            // 获取messageSource中的键
            String message = bindingResult.getFieldError().getDefaultMessage();
            // 根据messageSource键获取值
            throw new ParameterValidException(messageSource.getMessage(message, null, Locale.getDefault()));
        }

        boolean edit = dictionaryValueServices.edit(dictionaryValue);

        if (!edit) {
            throw new CRUDException("数据修改失败");
        }

        ResultDTO resultDTO = new ResultDTO();
        resultDTO.setResult(ResultDTO.SUCCESS);

        return resultDTO;
    }
}
